package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlCourse entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlCourse
 * @author MyEclipse Persistence Tools
 */

public class MdlCourseDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlCourseDAO.class);
	// property constants
	public static final String CATEGORY = "category";
	public static final String SORTORDER = "sortorder";
	public static final String FULLNAME = "fullname";
	public static final String SHORTNAME = "shortname";
	public static final String IDNUMBER = "idnumber";
	public static final String SUMMARY = "summary";
	public static final String SUMMARYFORMAT = "summaryformat";
	public static final String FORMAT = "format";
	public static final String SHOWGRADES = "showgrades";
	public static final String MODINFO = "modinfo";
	public static final String NEWSITEMS = "newsitems";
	public static final String STARTDATE = "startdate";
	public static final String NUMSECTIONS = "numsections";
	public static final String MARKER = "marker";
	public static final String MAXBYTES = "maxbytes";
	public static final String LEGACYFILES = "legacyfiles";
	public static final String SHOWREPORTS = "showreports";
	public static final String VISIBLE = "visible";
	public static final String VISIBLEOLD = "visibleold";
	public static final String HIDDENSECTIONS = "hiddensections";
	public static final String GROUPMODE = "groupmode";
	public static final String GROUPMODEFORCE = "groupmodeforce";
	public static final String DEFAULTGROUPINGID = "defaultgroupingid";
	public static final String LANG = "lang";
	public static final String THEME = "theme";
	public static final String TIMECREATED = "timecreated";
	public static final String TIMEMODIFIED = "timemodified";
	public static final String REQUESTED = "requested";
	public static final String RESTRICTMODULES = "restrictmodules";
	public static final String ENABLECOMPLETION = "enablecompletion";
	public static final String COMPLETIONSTARTONENROL = "completionstartonenrol";
	public static final String COMPLETIONNOTIFY = "completionnotify";

	public void save(MdlCourse transientInstance) {
		Transaction trx = getSession().beginTransaction();
		System.out.println("saving MdlCourse instance");
		try {
			getSession().save(transientInstance);
			trx.commit();
			System.out.println("save successful");
			trx=null;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MdlCourse persistentInstance) {
		System.out.println("deleting MdlCourse instance");
		try {
			getSession().delete(persistentInstance);
			System.out.println("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlCourse findById(java.lang.Long id) {
		System.out.println("getting MdlCourse instance with id: " + id);
		try {
			MdlCourse instance = (MdlCourse) getSession().get(
					"idv.loveloser.dao.MdlCourse", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlCourse instance) {
		System.out.println("finding MdlCourse instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlCourse")
					.add(Example.create(instance)).list();
			System.out.println("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		System.out.println("finding MdlCourse instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MdlCourse as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCategory(Object category) {
		return findByProperty(CATEGORY, category);
	}

	public List findBySortorder(Object sortorder) {
		return findByProperty(SORTORDER, sortorder);
	}

	public List findByFullname(Object fullname) {
		return findByProperty(FULLNAME, fullname);
	}

	public List findByShortname(Object shortname) {
		return findByProperty(SHORTNAME, shortname);
	}

	public List findByIdnumber(Object idnumber) {
		return findByProperty(IDNUMBER, idnumber);
	}

	public List findBySummary(Object summary) {
		return findByProperty(SUMMARY, summary);
	}

	public List findBySummaryformat(Object summaryformat) {
		return findByProperty(SUMMARYFORMAT, summaryformat);
	}

	public List findByFormat(Object format) {
		return findByProperty(FORMAT, format);
	}

	public List findByShowgrades(Object showgrades) {
		return findByProperty(SHOWGRADES, showgrades);
	}

	public List findByModinfo(Object modinfo) {
		return findByProperty(MODINFO, modinfo);
	}

	public List findByNewsitems(Object newsitems) {
		return findByProperty(NEWSITEMS, newsitems);
	}

	public List findByStartdate(Object startdate) {
		return findByProperty(STARTDATE, startdate);
	}

	public List findByNumsections(Object numsections) {
		return findByProperty(NUMSECTIONS, numsections);
	}

	public List findByMarker(Object marker) {
		return findByProperty(MARKER, marker);
	}

	public List findByMaxbytes(Object maxbytes) {
		return findByProperty(MAXBYTES, maxbytes);
	}

	public List findByLegacyfiles(Object legacyfiles) {
		return findByProperty(LEGACYFILES, legacyfiles);
	}

	public List findByShowreports(Object showreports) {
		return findByProperty(SHOWREPORTS, showreports);
	}

	public List findByVisible(Object visible) {
		return findByProperty(VISIBLE, visible);
	}

	public List findByVisibleold(Object visibleold) {
		return findByProperty(VISIBLEOLD, visibleold);
	}

	public List findByHiddensections(Object hiddensections) {
		return findByProperty(HIDDENSECTIONS, hiddensections);
	}

	public List findByGroupmode(Object groupmode) {
		return findByProperty(GROUPMODE, groupmode);
	}

	public List findByGroupmodeforce(Object groupmodeforce) {
		return findByProperty(GROUPMODEFORCE, groupmodeforce);
	}

	public List findByDefaultgroupingid(Object defaultgroupingid) {
		return findByProperty(DEFAULTGROUPINGID, defaultgroupingid);
	}

	public List findByLang(Object lang) {
		return findByProperty(LANG, lang);
	}

	public List findByTheme(Object theme) {
		return findByProperty(THEME, theme);
	}

	public List findByTimecreated(Object timecreated) {
		return findByProperty(TIMECREATED, timecreated);
	}

	public List findByTimemodified(Object timemodified) {
		return findByProperty(TIMEMODIFIED, timemodified);
	}

	public List findByRequested(Object requested) {
		return findByProperty(REQUESTED, requested);
	}

	public List findByRestrictmodules(Object restrictmodules) {
		return findByProperty(RESTRICTMODULES, restrictmodules);
	}

	public List findByEnablecompletion(Object enablecompletion) {
		return findByProperty(ENABLECOMPLETION, enablecompletion);
	}

	public List findByCompletionstartonenrol(Object completionstartonenrol) {
		return findByProperty(COMPLETIONSTARTONENROL, completionstartonenrol);
	}

	public List findByCompletionnotify(Object completionnotify) {
		return findByProperty(COMPLETIONNOTIFY, completionnotify);
	}

	public List findAll() {
		System.out.println("finding all MdlCourse instances");
		try {
			String queryString = "from MdlCourse";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			System.out.println("find all failed"+ re.toString());
			throw re;
		}
	}

	public MdlCourse merge(MdlCourse detachedInstance) {
		System.out.println("merging MdlCourse instance");
		try {
			MdlCourse result = (MdlCourse) getSession().merge(detachedInstance);
			System.out.println("merge successful");
			return result;
		} catch (RuntimeException re) {
			System.out.println("merge failed"+ re.toString());
			throw re;
		}
	}

	public void attachDirty(MdlCourse instance) {
		System.out.println("attaching dirty MdlCourse instance");
		try {
			getSession().saveOrUpdate(instance);
			System.out.println("attach successful");
		} catch (RuntimeException re) {
			System.out.println("attach failed"+ re.toString());
			throw re;
		}
	}

	public void attachClean(MdlCourse instance) {
		System.out.println("attaching clean MdlCourse instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			System.out.println("attach successful");
		} catch (RuntimeException re) {
			System.out.println("attach failed"+ re.toString());
			throw re;
		}
	}
}